const { Sequelize, DataTypes, Op } = require('sequelize')

// 中文文档
// https://github.com/demopark/sequelize-docs-Zh-CN/tree/master

// 建立连接
const sequelize = new Sequelize(
    'node_test', 'root', 'guzhan321',
    {
        host: 'localhost',
        dialect: 'mysql'
    }
)

// 创建模型
const GamePlayer = sequelize.define(   // 多
    'GamePlayer',
    {name: DataTypes.STRING}
)
const Team = sequelize.define(  // 1
    'Team',
    { name: DataTypes.STRING }
)

/*
    购物车对象
        - 用户
        - [产品]
*/

// 建立一对多关系
GamePlayer.belongsTo(Team)  // 1端      5个玩家为一组
Team.hasMany(GamePlayer)    // 多端     

;(async () => {
    // 同步模型
    await sequelize.sync({ force: true });

    // 插入数据
    console.log('1. 插入数据')
    let team = await Team.create({ name: '战国' })
    let moganna = await GamePlayer.create({ name: '莫甘娜', TeamId: team.get().id })
    let nv = await GamePlayer.create({ name: '薇恩', TeamId: team.get().id })
    let jiqiren = await GamePlayer.create({ name: '机器人', TeamId: team.get().id })

    // console.log(
    //     team.get()
    // )
    // console.log(
    //     moganna.get(),
    //     nv.get(),
    //     jiqiren.get(),
    // )

    // 链表查询
    // let players = await GamePlayer.findAll({ include: [ Team ] })
    // console.log(
    //     JSON.stringify(players, null, 2)
    // )

    // team = await Team.findAll({ where: { name: '战国' }, include: [ GamePlayer ] })
    // console.log(
    //     JSON.stringify(team, null, 2)
    // )

})();



